<?php



include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");

include_once($_SERVER["DOCUMENT_ROOT"] . "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/album/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();
if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	cgi::both($g_cgival['t'], "t", "json");
		
	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	cgi::both($g_cgival["id"], "id", "");
	$g_cgival["id"] = string::un_html($g_cgival["id"]);
	
	
	cgi::both($g_cgival["type"], "type", "add");
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$type = $g_cgival["type"];
	$id = $g_cgival["id"];
	if(!$id)
	{
		throw new Exception("id不能为空！");
	}
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["current_user_info"] = $user_detail_info;
		$g_show["is_logined"] = true;
	}
	
	
	
	$db_album_user_love = new db_album_user_love();
	$ip = IP::get_client_ip();
	if($type == "add")
	{
		
		$key = $uid.$ip.$id;
		$crc32_key = sprintf("%u",crc32($key));
		
		$ret = $db_album_user_love->insert_album_user_love($id,$uid,$crc32_key);
		if($ret)
		{
			
			$db_album_info = new db_album_info();
			$item = $db_album_info->get_album_item($id);
			
			$level ="";
			if(item)
			{
				foreach($config["album_level_need"]  as $key => $value)
				{
					if($item["count_love"] + 1 < $value)
					{
						$level = $key -1 ;
						break;
					}
				}
				
				
				//更新album
				$db_album_info->update_album_info_by_id($id,$uid="",$title="",$desc="",$pic1="",$pic2="",$pic3="",$pic4="",$status_count="",$status="",$show_type="",$body_url="",$body_attach="",$id1="",$id2="",$id3="",$id4="",$count_love=1,"","",$level);
				
			}
			
			
		}
		else
		{
			throw new Exception("已经喜欢过了！");
			
		}
		$g_show["errmsg"] = '喜欢+1';
	}
	elseif($type == "del")
	{
		$ret = $db_album_user_love->update_album_user_love_by_uid_and_album_id($id,$uid,0);
		
		$g_show["errmsg"] = '删除成功！';
	}
	elseif($type == "good")
	{
		$db_album_user_good = new db_album_user_good();
		
		
		$key = $uid.$id.$ip.date("Y-m-d");
		
		$crc32_key = sprintf("%u",crc32($key));
		$ret = $db_album_user_good->insert_album_user_good($id,$uid,$crc32_key);
		
		if($ret)
		{
			
			$db_album_info = new db_album_info();
			$item = $db_album_info->get_album_item($id);
			
			if(item)
			{
				//更新album
				$db_album_info->update_album_info_by_id($id,$uid="",$title="",$desc="",$pic1="",$pic2="",$pic3="",$pic4="",$status_count="",$status="",$show_type="",$body_url="",$body_attach="",$id1="",$id2="",$id3="",$id4="",$count_love="","",1);
				
			}
			
			
			
			
		}
		else
		{
			throw new Exception("今天已经顶起过了！");
			
		}
		$g_show["errmsg"] = '顶起+1';
		
		
		
	}
	
	
	

}

$g_show["error"] = 0;

try
{
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}


